﻿using Catastrophe.AutoSulution.EDC;
using Catastrophe.AutoSulution.TaskM;
using Catastrophe.BasePC.UI.DbDal;
using Catastrophe.BasePC.UI.File;
using Catastrophe.BasePC.UI.Model;
using Catastrophe.BasePC.UI.SystemIni;
using Catastrophe.Common.FileM;
using Catastrophe.Common.ObjM;
using Catastrophe.CommonCore.FileM;
using Catastrophe.Initialize;
using Catastrophe.Log;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Catastrophe.BasePC.UI.UIMode
{
    /// <summary>
    /// 登录
    /// </summary>
    public partial class LoadModel : ObservableObject
    {
        /// <summary>
        /// 用户名
        /// </summary>
        [ObservableProperty]
        private string _userName;
        /// <summary>
        /// 密码
        /// </summary>
        [ObservableProperty]
        private string _password;
        /// <summary>
        /// 加载信息
        /// </summary>
        [ObservableProperty]
        private string loadInfo;
        /// <summary>
        /// 校验结果
        /// </summary>

        [ObservableProperty]
        private bool checkValue;

        /// <summary>
        /// 标题
        /// </summary>
        [ObservableProperty]
        private string loginTittle;

        /// <summary>
        /// 宽
        /// </summary>
        [ObservableProperty]
        private int wdWidth;

        /// <summary>
        /// 高
        /// </summary>
        [ObservableProperty]
        private int wdHeight;

        /// <summary>
        /// 登录
        /// </summary>
        public ICommand Load { get; set; }
        /// <summary>
        /// 登录事件
        /// </summary>
        public event Action<SysUserEntity, bool> LoginHandler;
        /// <summary>
        /// 用户
        /// </summary>
        private SysUserDal _baseUserDal;
        /// <summary>
        /// 配置
        /// </summary>
        public FileConfigEntity? config { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ICommand LoadConfig { get; set; }
        public LoadModel()
        {
            this._baseUserDal = new SysUserDal();
            UserName = "admin";
            Password = "2357";

            config = FileConfigIni.GetFileConfig;
            Load = new RelayCommand(LoadMethod);
            LoadConfig = new RelayCommand(LoadConfigMethod);
            IniConfig();
        }

        private void LoadConfigMethod()
        {
            config = new FileConfigManager<FileConfigEntity>(FileTypeEnum.Json).ReadEntity();
            IniConfig();
        }

        /// <summary>
        /// 设置配置
        /// </summary>
        private void IniConfig()
        {
            LoginTittle = config.LoginTittle;
        }
        private void LoadMethod()
        {
            var checkvalue = _baseUserDal.CheckLogin(UserName, Password);
            if (checkvalue!=null)
            {
                FrameEnvironment.UserPwd = UserName;
                FrameEnvironment.IsAuthenticated = true;
                Ggo<SysUserEntity>.GetObj = checkvalue;
                Tuple<Task, Exception> tuple = TaskManager.StartTo(delegate
                {
                    SysOprRecordsDal.AddRecords(new SysOprRecordsEntity
                    {
                        Operator = checkvalue.UserName,
                        Module = "系统登录",
                        Result = "登录成功"
                    });
                });
                if (tuple.Item2 != null)
                {
                    ExceptionUtil.ShowDialog(tuple.Item2, "操作记录失败请查看Log配置!");
                }
                else
                {
                    checkValue = true;
                }
            }
            LoginHandler?.Invoke(checkvalue, checkValue);
        }
    }
}
